/**
 * @author 小泽玛丽罗
 */


/*界面模版*/
var tmpl_no_com='<div  class="ub umh5 unn b-gra c-wh uc-t1 uc-b1 ub-ver" style="border:1px solid #c5c5c5;3px 3px 6px rgba(180, 178, 178, 0.76) ">'
		+'<div class="ub">'
		+'<div class="umw3 umh5 umar-a"><img style="width:5em;height:4em" src="${HeadPic}"/></div>'
			+'<div class="ub ub-f1 ub-ver">'
				+'<div class="ub" style="margin-top:0.4em;">'
					+'<div class="ulev1 t-blu">${username}</div>'
					+'<div class="umw1"></div>'
					+'<div class="ub-f1"></div>'
					+'<div class="umw1"></div>'
				+'</div>'
				+'<div class="t-gra" style="font-size:0.7em;margin-top:0.8em ">粉丝数：1211</div>'
					+'<div class="t-gra" style="font-size:0.7em; margin-top:-1.2em;margin-left:7em">帖子数：1011</div>'
					+'<div class="t-gra " style="font-size:0.7em; margin-top:-1.6em;margin-left:14em"><img id=jiaguanzhu src="css/image/jiaguanzhu.png"/></div>' 
					+'<div class="ub-f1 ut-s t-gra" onclick="" style="font-size:0.7em;margin-top:0.5em;margin-bottom:0.3em; letter-spacing:0.15em">${content}</div>'
					+'<div class="umw1"></div>'
					+'<div class="ub-f1"></div>'
					+'<div class="umw1"></div>'
				+'</div>'
			+'</div>'
		+'</div>';
var tmpl_listCategory='<div class="ub ub-ver">'
		                	+'<div class="ub-f1 c-m1  ver-c15 divBT divBB" style="min-height:1.5em;background-color:#f1f1f1;font-weight:bold;">${title}</div>'
							+'<div id="${title}" class="ub">'
								
							+'</div>'
		                +'</div>'
var tmpl_ChildListCategory='<div class="ub-f1 min-h3 ver-c3" style="padding-top:0.5em;">'
								+'<div id="${category_id}" ontouchstart="zy_touch('+"'btn-newact'"+')" onclick="loadWeiba(this)" name="${title}" class="min-h2 divB max-h2 ver-c2 tx-c max-w5" style="width:90%;margin:auto;border-radius:2em;background-color:#f9f9f9;">${title}</div>'
							+'</div>';				
 var tmpl_add='<div id="add" name="Weibo" onclick="clickAdd(this)" class="ub umh5  unn c-wh tx-c ub-ver" style="border:1px solid #c5c5c5;background-color:#ebebeb; ">'
		+'<div ontouchstart="zy_touch('+'btn-act'+')" class="btn  uinn5 uc-a t-bla">点击加载</div>'
 +'</div>';
 
 var tmpl_weiba_follow='<div  class="ub umh5 max-h5 unn b-gra c-wh uc-t1 uc-b1 ub-ver"  ontouchstart="zy_touch('+"'btn-newact'"+')" style="border:1px solid #c5c5c5;3px 3px 6px rgba(180, 178, 178, 0.76) ">'
		+'<div class="ub max-h5" id="bid" >'
			+'<div id="${weiba_id}" name="${title}" class="ub-f1 max-h4 max-w4 umar-a" onclick="loadPosts(this)"><img style="width:100%;" src="css/img/headBig.png"/></div>'
			+'<div id="${weiba_id}" name="${title}" class="ub ub-f4 ub-ver" onclick="loadPosts(this)">'
				+'<div class="ub ub-f1"  style="margin-top:0.4em;" >'
					+'<div class="ulev1 t-blu">${title}</div>'
					+'<div class="umw1"></div>'
					+'<div class="ub-f1"></div>'
					+'<div class="umw1"></div>'
				+'</div>'
				+'<div class="ub ub-f1"  style="margin-top:0.4em;">'
					+'<div class="ub-f4 ub ulev-1">'
						+'<div class="ub-f2 t-gra">粉丝数：${follower_count} &nbsp;帖子数：${post_count}</div>'
					+'</div>'
					+'<div class="ub-f1"></div>'
					
				+'</div>'
				+'<div class="ub ub-f1 ut-s ulev-1 t-gra" style="margin-top:0.4em; width:13em;">'
					+'${intro}'
				+'</div>'
			+'</div>'
			+'<div class=" ub ub-f1 ub-ver">'
				+'<div class="ub-f1" style="min-height:0.9em;"></div>'
				+'<div class="ub-f2 ver-c">'
					+'<div id="${weiba_id}" name="${title}" class="ulev tx-c t-wh min-h2 min-w4 max-w4 ver-c " onclick="attention(this)" style="background-color:#db4937;margin-right:0.2em;">关  注</div>'
					+'</div>'
				+'<div class="ub-f1"></div>'
			+'</div>'
		+'</div>'
	+'</div>'
 var tmpl_weiba_unfollow='<div  class="ub umh5 max-h5 unn b-gra c-wh uc-t1 uc-b1 ub-ver"  ontouchstart="zy_touch('+"'btn-newact'"+')" style="border:1px solid #c5c5c5;3px 3px 6px rgba(180, 178, 178, 0.76) ">'
		+'<div class="ub max-h5" id="bid" >'
			+'<div id="${weiba_id}" name="${title}" class="ub-f1 max-h4 max-w4 umar-a" onclick="loadPosts(this)"><img style="width:100%;" src="css/img/headBig.png"/></div>'
			+'<div id="${weiba_id}" name="${title}" class="ub ub-f4 ub-ver" onclick="loadPosts(this)">'
				+'<div class="ub ub-f1"  style="margin-top:0.4em;" >'
					+'<div class="ulev1 t-blu">${title}</div>'
					+'<div class="umw1"></div>'
					+'<div class="ub-f1"></div>'
					+'<div class="umw1"></div>'
				+'</div>'
				+'<div class="ub ub-f1"  style="margin-top:0.4em;">'
					+'<div class="ub-f4 ub ulev-1">'
						+'<div class="ub-f2 t-gra">粉丝数：${follower_count} &nbsp;帖子数：${post_count}</div>'
					+'</div>'
					+'<div class="ub-f1"></div>'
					
				+'</div>'
				+'<div class="ub ub-f1 ut-s ulev-1 t-gra" style="margin-top:0.4em; width:13em;">'
					+'${intro}'
				+'</div>'
			+'</div>'
			+'<div class=" ub ub-f1 ub-ver">'
				+'<div class="ub-f1" style="min-height:0.9em;"></div>'
				+'<div class="ub-f2 ver-c">'
					+'<div id="${weiba_id}" name="${title}" class="ulev tx-c t-wh min-h2 min-w4 max-w4 ver-c " onclick="attention(this)" style="background-color:rgb(117, 113, 113);margin-right:0.2em;">取消关注</div>'
					+'</div>'
				+'<div class="ub-f1"></div>'
			+'</div>'
		+'</div>'
	+'</div>'
 var weiboListUrl='http://www.diaoyumi.ourstu.com/api/weiba/listWeiba';
 var listCategoryUrl='http://www.diaoyumi.ourstu.com/api/weiba/listCategory';
 var weibaPostUrl='http://www.diaoyumi.ourstu.com/api/weiba/listPost';
 function loadListCategory(){
 	var httpID=132123;
	uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				var listCount=zy_tmpl_count(json["tree"]);
				for(var i=0;i<listCount;i++){
					var obj=json["tree"][i];
					var div=document.createElement("DIV");
					var titleHead=zy_tmpl_s(tmpl_listCategory,obj,zy_tmpl_count(obj));
					div.innerHTML=titleHead;
					$$("content").appendChild(div);
					//var childCount=zy_tmpl_count(obj["child"]);
					for(var j=0;j<2;j++){
						var objc=obj["child"];
						var divc=document.createElement("DIV");
						divc.setAttribute("class","ub-f1");
						var child=zy_tmpl_s(tmpl_ChildListCategory,objc);
						divc.innerHTML=child;
						$$(obj["title"]).appendChild(divc);
					}
					uexXmlHttpMgr.close(httpID);
				}
			}else{
				uexWindow.toast(1,5,json["message"],1000);
				uexXmlHttpMgr.close(httpID);
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'post',listCategoryUrl,3000);
	uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
	uexXmlHttpMgr.send(httpID);
 }
 
 function loadWeiba(obj){
 	var category_id=obj.id;
	var category_name=obj.getAttribute("name");
	window.localStorage.setItem("category_id",category_id);
	window.localStorage.setItem("category_name",category_name);
	uexWindow.open("weibaindex",0,"weibaindex.html",1,"","",0,350);
 }
 function loadWeibaList(){
 	
 	var httpID=13439;
	uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				var weibaCount=zy_tmpl_count(json["list"]);
				for(var i=0;i<weibaCount;i++){
					var div=document.createElement("DIV");
					var obj=json["list"][i];
					if(obj["is_followed"]){
						var weiba=zy_tmpl_s(tmpl_weiba_unfollow,obj);
						div.innerHTML=weiba;
						document.body.appendChild(div);
					}else{
						var weiba=zy_tmpl_s(tmpl_weiba_follow,obj);
						div.innerHTML=weiba;
						document.body.appendChild(div);
					}
					
					
					
				}
				uexXmlHttpMgr.close(httpID);
			}else{
				uexWindow.toast(1,5,json["message"],1000);
				uexXmlHttpMgr.close(httpID);
			}
		}
	}
	uexXmlHttpMgr.open(httpID,'post',weiboListUrl,3000);
	uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"category_id",parseInt(window.localStorage["category_id"]));
	alert(parseInt(window.localStorage["category_id"]));
	uexXmlHttpMgr.send(httpID);
 }
 
 function loadPosts(obj){
 	var weiba_id=obj.id;
	var weiba_name=obj.getAttribute("name");
	window.localStorage.setItem("weiba_id",weiba_id);
	window.localStorage.setItem("weiba_name",weiba_name);
	uexWindow.open("microbar",0,"microbar.html",1,"","",0,250);
 }
 
 function loadWeibaPost(){
 	var httpID=47238;
	uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				uexXmlHttpMgr.close(httpID);
				var lens=zy_tmpl_count(json["list"]);
				for(var i=0;i<lens;i++){
					var obj=json["list"][i];
					var obj_picNum=parseInt(zy_tmpl_count(obj["picture_list"]));
					if(obj_picNum!=0){
						if(obj_picNum>=3){
							var div=document.createElement("DIV");
							obj=picUpLevel(obj,3);
							obj["nickname"]=obj["user"].nickname;
							var post=zy_tmpl_s(tmpl_post_threeSmallPic,obj);//三张小
							div.innerHTML=post;
							document.body.appendChild(div);
						}else if(obj_picNum==1){
							obj=picUpLevel(obj,1);
							obj["nickname"]=obj["user"].nickname;
							var div=document.createElement("DIV");
							var post=zy_tmpl_s(tmpl_post_oneSmall,obj);//一张小
							div.innerHTML=post;
							document.body.appendChild(div);
						}else if(obj_pinNum==2){
							obj=picUpLevel(obj,2);
							obj["nickname"]=obj["user"].nickname;
							var div=document.createElement("DIV");
							var post=zy_tmpl_s(tmpl_post_twoSmall,obj);//二张小
							div.innerHTML=post;
							document.body.appendChild(div);
						}
					}else{
						obj["nickname"]=obj["user"].nickname;
						var div=document.createElement("DIV");
						var post=zy_tmpl_s(tmpl_post_nopic,obj);//无图
						div.innerHTML=post;
						document.body.appendChild(div);
					}
				}
			}
		}else{
				uexWindow.toast(1,5,json["message"],1000);
				uexXmlHttpMgr.close(httpID);
			}
		}
	var weiba_id=parseInt(window.localStorage["weiba_id"]);
	uexXmlHttpMgr.open(httpID,'post',weibaPostUrl,3000);
	uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
	uexXmlHttpMgr.setPostData(httpID,'0',"weiba_id",parseInt(window.localStorage["weiba_id"]));
	uexXmlHttpMgr.send(httpID);
}
/**
 * @argument 将obj子元素内的picture提到第一级
 * @param {Object} obj
 * @param {Object} num
 */
function picUpLevel(obj,num){
	for(var i=0;i<num;i++){
		var objpic=obj["picture_list"][i];
		obj["pic"+i]=objpic["small"];
	}
	return obj;
} 
function loadPost(){
	
}

function follow(obj,type){
	var weiba_id=obj.id;
	var followUrl="http://www.diaoyumi.ourstu.com/api/weiba/followWeiba";
	var unfollowUrl="http://www.diaoyumi.ourstu.com/api/weiba/unfollowWeiba";
	var httpID=996;
	uexXmlHttpMgr.onData=function(inOpCode,inStatus,inResult){
		if(inStatus==1){
			var json=eval("("+inResult+")");
			if(json["success"]){
				if (type) {
					uexWindow.toast(1,5,"关注成功",1000);
					obj.style.backgroundColor = "rgb(117, 113, 113)";
					obj.innerHTML = "取消关注";
				}else{
					uexWindow.toast(1,5,"取消关注成功",1000);
					obj.style.backgroundColor="rgb(201, 9, 9)";
					obj.innerHTML="关  注";
				}
			}
			uexXmlHttpMgr.close(httpID);
		}
	}
	if(type){//true 是关注
		uexXmlHttpMgr.open(httpID,'post',followUrl,3000);
		uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
		uexXmlHttpMgr.setPostData(httpID,'0',"weiba_id",weiba_id);
		uexXmlHttpMgr.send(httpID);
	}else{//false是取消关注
		uexXmlHttpMgr.open(httpID,'post',unfollowUrl,3000);
		uexXmlHttpMgr.setPostData(httpID,'0',"session_id",window.localStorage["session_id"]);
		uexXmlHttpMgr.setPostData(httpID,'0',"weiba_id",weiba_id);
		uexXmlHttpMgr.send(httpID);
	}
	
}
 		